/* xxcstwosumup.i - Raw Material Puchase variance and FG variance rollup */      
/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* xxcstwosumup.i - Raw Material Puchase variance and FG variance rollup    */  
/* PROCEDURE TYPE        : Variance allocation program                      */
/* DESCRIPTION           : Variance allocation program                      */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               22-Aug-19   */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/
/* 2019  /* {1} */ entity "BJ20" /* {2} */ site 2019  /* {3} */ year 1  /* {4} */ period */ 
/* 1. Copy iss-wo rct-wo master table to xxcstwoallhist_hist                */
/* 2. base on tt_xxcstallhist iss-wo variance and total issue               */
/*    wo quantity to calculate RAW allocation rate and amount               */
/* 3. Calculate Parent part raw amount                                      */
/* 4. Calculate Parent Part Raw variance allocation rate for                */
/*    each issue type                                                       */
/* 5. base on xxcstwoallhist to assign issue level (bom level)              */             

/*
{us/mf/mfdtitle.i "+1" }
*/

define variable l_max                          as int                 no-undo. 
define variable l_level                        as int                 no-undo. 
define variable l_par_level                    as int                 no-undo.
define variable cur_level                            as int initial 0       no-undo.
define variable v_max                          as int initial 0       no-undo.



define temp-table   tt_xxcstwoallhist like     xxcstwoallhist_hist.
define buffer       b_xxcstwoallhist  for      tt_xxcstwoallhist.

/* for input parameter */
DEFINE VARIABLE i_entity              LIKE  en_entity   FORMAT "x(8)"     NO-UNDO.
DEFINE VARIABLE i_site                LIKE  si_site                       NO-UNDO.
DEFINE VARIABLE i_year                LIKE  glc_year                      NO-UNDO.
DEFINE VARIABLE i_period              LIKE  glc_per                       NO-UNDO.
/*
message "GO" view-as alert-box.
*/
/* assign input parameter value */

  assign
  i_entity = {1}
  i_site   = {2}
  i_year   = {3}
  i_period = {4}.

  empty temp-table tt_xxcstwoallhist.


 /** create wo variance allocation detail start **/
 /** xxcstwoallhist_hist  is component issue and parent receipt detail
     first copy total issue and receipt quantity  
 	according xxcstissrctsum_mstr to create xxcstwoallhist_hist 
 	so xxcstwoallhist_hist maybe less than tt_xxcstallhist table record **/
 for each xxcstissrctsum_mstr where xxcstissrctsum_mstr.xxcstissrctsum_domain = global_domain
                                and xxcstissrctsum_mstr.xxcstissrctsum_site   = i_site
                                and xxcstissrctsum_mstr.xxcstissrctsum_year   = i_year
                                and xxcstissrctsum_mstr.xxcstissrctsum_period = i_period
                                and xxcstissrctsum_mstr.xxcstissrctsum_entity = i_entity
                                and xxcstissrctsum_mstr.xxcstissrctsum_iss_totqty <> 0 
								no-lock	.	
            totrqty = 0.  		
		if debug-prog then 	
      status default xxcstissrctsum_mstr.xxcstissrctsum_comp + " COPY xxcstissrctsum_mstr".
 create tt_xxcstwoallhist.
 
 assign tt_xxcstwoallhist.xxcstwoallhist_domain         = global_domain
        tt_xxcstwoallhist.xxcstwoallhist_site           = xxcstissrctsum_mstr.xxcstissrctsum_site
 	    tt_xxcstwoallhist.xxcstwoallhist_year           = xxcstissrctsum_mstr.xxcstissrctsum_year
 	    tt_xxcstwoallhist.xxcstwoallhist_period         = xxcstissrctsum_mstr.xxcstissrctsum_period
 	    tt_xxcstwoallhist.xxcstwoallhist_entity         = xxcstissrctsum_mstr.xxcstissrctsum_entity
 	    tt_xxcstwoallhist.xxcstwoallhist_comp           = xxcstissrctsum_mstr.xxcstissrctsum_comp
 	    tt_xxcstwoallhist.xxcstwoallhist_par            = xxcstissrctsum_mstr.xxcstissrctsum_par
 	    tt_xxcstwoallhist.xxcstwoallhist_iss_totqty     = xxcstissrctsum_mstr.xxcstissrctsum_iss_totqty
 	    tt_xxcstwoallhist.xxcstwoallhist_rct_totqty     = xxcstissrctsum_mstr.xxcstissrctsum_rct_totqty
 	    tt_xxcstwoallhist.xxcstwoallhist_comp_amount    = xxcstissrctsum_mstr.xxcstissrctsum_comp_amount
 	    tt_xxcstwoallhist.xxcstwoallhist_par_amount     = xxcstissrctsum_mstr.xxcstissrctsum_par_amount	   
 	    tt_xxcstwoallhist.xxcstwoallhist_comp_prod_line = xxcstissrctsum_mstr.xxcstissrctsum_comp_prod_line
 	    tt_xxcstwoallhist.xxcstwoallhist_par_prod_line  = xxcstissrctsum_mstr.xxcstissrctsum_par_prod_line.
 	 /* bug found xxcstwoallhist_par should be xxcstwoallhist_comp 20190902 GY */
	 if debug-prog then
	 if tt_xxcstwoallhist.xxcstwoallhist_comp = tt_xxcstwoallhist.xxcstwoallhist_par then message tt_xxcstwoallhist.xxcstwoallhist_par view-as alert-box.
	  Run GetTrQtySum(input tt_xxcstwoallhist.xxcstwoallhist_comp,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-WO",
	     	      output totrqty).
      Run GetSameISSRCTWO(input tt_xxcstwoallhist.xxcstwoallhist_comp,
                             input entity,
                             input site,
                             input li_year,
                             input li_period,
                             output totissqty).
				totrqty = totrqty - totissqty.
		 tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty  = - totrqty.
	  Run GetTrQtySum(input tt_xxcstwoallhist.xxcstwoallhist_par,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "RCT-WO",
	     	       output totrqty).
		 tt_xxcstwoallhist.xxcstwoallhist_rct_pertotqty  = totrqty.

 
 end. /* for each xxcstissrctsum_mstr Line 6 */
/** create wo variance allocation head  end **/   

/** retrive tt_xxcstallhist iss-wo purchase variance to calculate allocation rate **/
/** if purchase part have manufacturing variance the variable can't be allocated **/
  for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  = global_domain
 	                        and  tt_xxcstallhist.xxcstall_site    =  i_site
 				            and  tt_xxcstallhist.xxcstall_year    =  i_year
 				            and  tt_xxcstallhist.xxcstall_period  =  i_period
 				            and  tt_xxcstallhist.xxcstall_entity  =  i_entity
 				            .
   if (tt_xxcstallhist.xxcstall_curisswoppv        <> 0 or
 	   tt_xxcstallhist.xxcstall_curisswofreight    <> 0 or
 	   tt_xxcstallhist.xxcstall_curisswoapusage    <> 0 or
 	   tt_xxcstallhist.xxcstall_curisswoaprate     <> 0 or
 	   tt_xxcstallhist.xxcstall_curisswocstadj     <> 0 or
 	   tt_xxcstallhist.xxcstall_curisswocyccnt     <> 0 or
 	   tt_xxcstallhist.xxcstall_curisswocommon     <> 0 or
 	   tt_xxcstallhist.xxcstall_curisswoother      <> 0) then do:    	
      
        for each tt_xxcstwoallhist where tt_xxcstwoallhist.xxcstwoallhist_domain =  global_domain  
 	                                 and tt_xxcstwoallhist.xxcstwoallhist_site   =  tt_xxcstallhist.xxcstall_site 
 					                 and tt_xxcstwoallhist.xxcstwoallhist_comp   =  tt_xxcstallhist.xxcstall_part       
 					                 and tt_xxcstwoallhist.xxcstwoallhist_entity =  tt_xxcstallhist.xxcstall_entity
 					                 and tt_xxcstwoallhist.xxcstwoallhist_year   =  tt_xxcstallhist.xxcstall_year
 					                 and tt_xxcstwoallhist.xxcstwoallhist_period =  tt_xxcstallhist.xxcstall_period
                                     .
		if debug-prog then 							 
 		status default tt_xxcstwoallhist.xxcstwoallhist_comp + " COMPONET Rate amount".
      /** allocation rate = component purchase variance amount  /                  **/
          /** total iss-wo quantity tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty **/ 
 		assign 
 		        tt_xxcstwoallhist.xxcstwoallhist_rawrateppv         = tt_xxcstallhist.xxcstall_curisswoppv        
				                                                          /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
                tt_xxcstwoallhist.xxcstwoallhist_rawratefreight     = tt_xxcstallhist.xxcstall_curisswofreight   
                                                           				  /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawrateapusage     = tt_xxcstallhist.xxcstall_curisswoapusage    
				                                                          /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawrateaprate      = tt_xxcstallhist.xxcstall_curisswoaprate     
				                                                          /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj      = tt_xxcstallhist.xxcstall_curisswocstadj     
				                                                          /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_mtl  = tt_xxcstallhist.xxcstall_curisswocstadj_mtl 
				                                                          /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_lbr  = tt_xxcstallhist.xxcstall_curisswocstadj_lbr 
				                                                          /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_bdn  = tt_xxcstallhist.xxcstall_curisswocstadj_bdn 
				                                                          /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_ovh  = tt_xxcstallhist.xxcstall_curisswocstadj_ovh 
				                                                          /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_sub  = tt_xxcstallhist.xxcstall_curisswocstadj_sub 
				                                                          /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                /*
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt      = tt_xxcstallhist.xxcstall_curisswocyccnt       /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_mtl      = tt_xxcstallhist.xxcstall_curisswocyccnt_mtl       /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_lbr      = tt_xxcstallhist.xxcstall_curisswocyccnt_lbr       /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_bdn      = tt_xxcstallhist.xxcstall_curisswocyccnt_bdn       /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_ovh      = tt_xxcstallhist.xxcstall_curisswocyccnt_ovh       /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_sub      = tt_xxcstallhist.xxcstall_curisswocyccnt_sub       /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                */
				tt_xxcstwoallhist.xxcstwoallhist_rawratecommon      = tt_xxcstallhist.xxcstall_curisswocommon     /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                tt_xxcstwoallhist.xxcstwoallhist_rawrateothers      = tt_xxcstallhist.xxcstall_curisswoother      /  tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty
                
                tt_xxcstwoallhist.xxcstwoallhist_rawrateppv        = if   tt_xxcstwoallhist.xxcstwoallhist_rawrateppv          = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawrateppv               
                tt_xxcstwoallhist.xxcstwoallhist_rawratefreight	   = if   tt_xxcstwoallhist.xxcstwoallhist_rawratefreight	   = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratefreight     
                tt_xxcstwoallhist.xxcstwoallhist_rawrateapusage	   = if   tt_xxcstwoallhist.xxcstwoallhist_rawrateapusage	   = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawrateapusage     
                tt_xxcstwoallhist.xxcstwoallhist_rawrateaprate	   = if   tt_xxcstwoallhist.xxcstwoallhist_rawrateaprate	   = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawrateaprate      
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj	   = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj	   = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj      
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_mtl = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_mtl   = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_mtl  
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_lbr = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_lbr   = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_lbr  
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_bdn = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_bdn   = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_bdn  
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_ovh = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_ovh   = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_ovh  
                tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_sub = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_sub   = ? 
				                                                         then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_sub   
                /*
				tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt	   = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt	   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt      .
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_mtl = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_mtl   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt      .
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_lbr = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_lbr   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt      .
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_bdn = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_bdn   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt      .
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_ovh = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_ovh   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt      .
                tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_sub = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_sub   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt      .
                */
				tt_xxcstwoallhist.xxcstwoallhist_rawratecommon	   = if   tt_xxcstwoallhist.xxcstwoallhist_rawratecommon	   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawratecommon      
                tt_xxcstwoallhist.xxcstwoallhist_rawrateothers	   = if   tt_xxcstwoallhist.xxcstwoallhist_rawrateothers	   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_rawrateothers      
                 
				 .
		   /** calculate iss-wo alllocate amount for parent part */
           assign
                tt_xxcstwoallhist.xxcstwoallhist_rawcurppv        = tt_xxcstwoallhist.xxcstwoallhist_rawrateppv        * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurfreight    = tt_xxcstwoallhist.xxcstwoallhist_rawratefreight    * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurapusage    = tt_xxcstwoallhist.xxcstwoallhist_rawrateapusage    * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcuraprate     = tt_xxcstwoallhist.xxcstwoallhist_rawrateaprate     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcstadj     = tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcstadj_mtl = tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_mtl * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcstadj_lbr = tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_lbr * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcstadj_bdn = tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_bdn * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcstadj_ovh = tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_ovh * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcstadj_sub = tt_xxcstwoallhist.xxcstwoallhist_rawratecstadj_sub * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                /*                                                                                                                                    
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcyccnt     = tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcyccnt_mtl = tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_mtl * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcyccnt_lbr = tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_lbr * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcyccnt_bdn = tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_bdn * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcyccnt_ovh = tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_ovh * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurcyccnt_sub = tt_xxcstwoallhist.xxcstwoallhist_rawratecyccnt_sub * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                */                                                                                                       
				tt_xxcstwoallhist.xxcstwoallhist_rawcurcommon     = tt_xxcstwoallhist.xxcstwoallhist_rawratecommon     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurothers     = tt_xxcstwoallhist.xxcstwoallhist_rawrateothers     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty)
                tt_xxcstwoallhist.xxcstwoallhist_rawcurtot        = tt_xxcstwoallhist.xxcstwoallhist_rawcurppv        +      
	                                                                tt_xxcstwoallhist.xxcstwoallhist_rawcurfreight    +
	                                                                tt_xxcstwoallhist.xxcstwoallhist_rawcurapusage    +
	                                                                tt_xxcstwoallhist.xxcstwoallhist_rawcuraprate     +
	                                                                tt_xxcstwoallhist.xxcstwoallhist_rawcurcstadj     +
                                                                    /*
																	tt_xxcstwoallhist.xxcstwoallhist_rawcurcyccnt     +
																	*/
                                                                    tt_xxcstwoallhist.xxcstwoallhist_rawcurcommon     +
                                                                    
																    tt_xxcstwoallhist.xxcstwoallhist_rawcurothers   . 

              end. /** for each tt_xxcstwoallhist **/			   
         end. /**  if (tt_xxcstallhist.xxcstall_curisswoppv        <> 0 or **/
     end. /** for each tt_xxcstallhist **/

    /** above calculate result is component issue to each parent amount           **/
	/** Accumulate Parent Part Purchase Current Variance to tt_xxcstallhist table **/
		
    for each tt_xxcstwoallhist     where tt_xxcstwoallhist.xxcstwoallhist_site   =  i_site
 					                 and tt_xxcstwoallhist.xxcstwoallhist_domain =  global_domain         
 					                 and tt_xxcstwoallhist.xxcstwoallhist_entity =  i_entity
 					                 and tt_xxcstwoallhist.xxcstwoallhist_year   =  i_year
 					                 and tt_xxcstwoallhist.xxcstwoallhist_period =  i_period
                                     .
		if debug-prog then 							 
		status default tt_xxcstwoallhist.xxcstwoallhist_par + " Accumulate RAW amount to Par".	
			find tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  = global_domain
 	                             and   tt_xxcstallhist.xxcstall_site    =  i_site
 				                 and   tt_xxcstallhist.xxcstall_year    =  i_year
 				                 and   tt_xxcstallhist.xxcstall_period  =  i_period
 				                 and   tt_xxcstallhist.xxcstall_entity  =  i_entity	
                                 and   tt_xxcstallhist.xxcstall_part    = tt_xxcstwoallhist.xxcstwoallhist_par
                                no-error.
								
            if avail tt_xxcstallhist then             
            assign
			         tt_xxcstallhist.xxcstall_scurraw = tt_xxcstallhist.xxcstall_scurraw + tt_xxcstwoallhist.xxcstwoallhist_rawcurtot.			
     end. /* for each tt_xxcstwoallhist where tt_xxcstwoallhist.xxcstwoallhist_par   */
  
  /** Calculate Parent Purchase Variance allocation rate and amount **/
  for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  =  global_domain
 	                       and   tt_xxcstallhist.xxcstall_site    =  i_site
 				           and   tt_xxcstallhist.xxcstall_year    =  i_year
 				           and   tt_xxcstallhist.xxcstall_period  =  i_period
 				           and   tt_xxcstallhist.xxcstall_entity  =  i_entity	
						   .
	if debug-prog then 					   
	status default tt_xxcstallhist.xxcstall_part + " Parent RAW rate amount".						   
  /**Raw Variance Allocation Rate  = (begin raw variance + current raw variance)  **/
  /**      / (begin inventory + current rct inventory) **/	 
      totBeginvqty = 0.
	  totRctinvqty = 0.
	  if debug-prog then 
	  status default tt_xxcstallhist.xxcstall_part + " Calculate Raw Rate".
	  
	  Run GetBegInvQty(input tt_xxcstallhist.xxcstall_part,
	                   input tt_xxcstallhist.xxcstall_entity, 
					   input tt_xxcstallhist.xxcstall_site, 
					   input tt_xxcstallhist.xxcstall_year, 
					   input tt_xxcstallhist.xxcstall_period, 
					   output totBeginvqty).
	 
	  Run GetTotalRctQty(input tt_xxcstallhist.xxcstall_part,
	                     input tt_xxcstallhist.xxcstall_entity, 
					     input tt_xxcstallhist.xxcstall_site, 
					     input tt_xxcstallhist.xxcstall_year, 
					     input tt_xxcstallhist.xxcstall_period, 
						 output totRctinvqty). 
  
	 
		tt_xxcstallhist.xxcstall_srateraw = (tt_xxcstallhist.xxcstall_sbegraw + tt_xxcstallhist.xxcstall_scurraw)
		                                     / (  totBeginvqty + totRctinvqty).
		tt_xxcstallhist.xxcstall_srateraw = if 	tt_xxcstallhist.xxcstall_srateraw  = ? then 0 else  tt_xxcstallhist.xxcstall_srateraw .
		
       /** Raw Variance Allocation amount  = Raw Variance amount * ISSUE quantity **/
	  totrqty = 0.
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-UNP",
	               output totrqty).
         tt_xxcstallhist.xxcstall_scurissunpraw =  tt_xxcstallhist.xxcstall_srateraw * (- totrqty).
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-WO",
	               output totrqty).
	         Run GetSameISSRCTWO(input tt_xxcstallhist.xxcstall_part,
                             input entity,
                             input site,
                             input li_year,
                             input li_period,
                             output totissqty).
				totrqty = totrqty - totissqty.	
         tt_xxcstallhist.xxcstall_scurissworaw  =  tt_xxcstallhist.xxcstall_srateraw * (- totrqty).
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "INVLOSS",
	               output totrqty).
         tt_xxcstallhist.xxcstall_scurcyccntraw =  tt_xxcstallhist.xxcstall_srateraw * (- totrqty).
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-SO",
	               output totrqty).
		 tt_xxcstallhist.xxcstall_scurisssoraw  =  tt_xxcstallhist.xxcstall_srateraw * (- totrqty).

    end. /* for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain */


               

/*****            *****/
/*****SFG Roll-up *****/
/*****            *****/
    v_max = 0.
  
    /**  Assign BOM Level for Component if Level is 0 means lowest level **/
     for each tt_xxcstwoallhist where  tt_xxcstwoallhist.xxcstwoallhist_domain = global_domain
                                   and tt_xxcstwoallhist.xxcstwoallhist_site   = i_site
                                   and tt_xxcstwoallhist.xxcstwoallhist_year   = i_year
                                   and tt_xxcstwoallhist.xxcstwoallhist_period = i_period
                                   and tt_xxcstwoallhist.xxcstwoallhist_entity = i_entity	
                                   break by tt_xxcstwoallhist.xxcstwoallhist_par
								   .
	/* when parent = component the program will get dead loop. */						   
    if tt_xxcstwoallhist.xxcstwoallhist_par = tt_xxcstwoallhist.xxcstwoallhist_comp then next.
	/*
    message "bom level " tt_xxcstwoallhist.xxcstwoallhist_comp view-as alert-box.	
	*/
	if debug-prog then 					   
      status default tt_xxcstwoallhist.xxcstwoallhist_par + " BOM Level".					   
      if first-of(tt_xxcstwoallhist.xxcstwoallhist_par) then
       assign
	     l_level = 1
         l_max = 0
         l_par_level = 1.
         
	   run  bom_level(input tt_xxcstwoallhist.xxcstwoallhist_comp, input l_level + 1).
	    if l_par_level > v_max then v_max = l_par_level.
     if last-of(tt_xxcstwoallhist.xxcstwoallhist_par) then 
     do:
          for each b_xxcstwoallhist where   b_xxcstwoallhist.xxcstwoallhist_domain = global_domain 
	                                    and b_xxcstwoallhist.xxcstwoallhist_site   = i_site
										and b_xxcstwoallhist.xxcstwoallhist_year   = i_year
	                                    and b_xxcstwoallhist.xxcstwoallhist_period = i_period
										and b_xxcstwoallhist.xxcstwoallhist_entity = i_entity
                                        and b_xxcstwoallhist.xxcstwoallhist_comp = tt_xxcstwoallhist.xxcstwoallhist_par.
            if tt_xxcstwoallhist.xxcstwoallhist_par = tt_xxcstwoallhist.xxcstwoallhist_comp 
			then  b_xxcstwoallhist.xxcstwoallhist_complevel = -1 .
			else
            b_xxcstwoallhist.xxcstwoallhist_complevel = l_par_level.
          end. /** for each xxcstissrctsum_mstr where    xxcstissrc **/
     end.   
    end.
	
	/*
	message v_max view-as alert-box.
	*/
/* BOM Level calculate */
 
 cur_level = 0.
 
/** retrive tt_xxcstallhist iss-wo Manufacture variance to calculate allocation rate **/
/** repeat to v_max **/
Repeat:
 
 for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  = global_domain
 	                        and tt_xxcstallhist.xxcstall_site    = i_site
 				            and tt_xxcstallhist.xxcstall_year    = i_year
 				            and tt_xxcstallhist.xxcstall_period  = i_period
 				            and tt_xxcstallhist.xxcstall_entity  = i_entity
 				            .
         if tt_xxcstallhist.xxcstall_scurisswomusage      <> 0 or
            tt_xxcstallhist.xxcstall_scurisswomrate       <> 0 or
            tt_xxcstallhist.xxcstall_scurisswommthd       <> 0 or
            tt_xxcstallhist.xxcstall_scurisswosubusage    <> 0 or
            tt_xxcstallhist.xxcstall_scurisswosubrate     <> 0 or
            tt_xxcstallhist.xxcstall_scurisswolbrusage    <> 0 or
            tt_xxcstallhist.xxcstall_scurisswolbrrate     <> 0 or
            tt_xxcstallhist.xxcstall_scurisswobdnusage    <> 0 or
            tt_xxcstallhist.xxcstall_scurisswobdnrate     <> 0 or
            tt_xxcstallhist.xxcstall_scurisswocstadj      <> 0 or
            tt_xxcstallhist.xxcstall_scurisswoothers      <> 0 or
			
            tt_xxcstallhist.xxcstall_scurisswocommon      <> 0 or
			/*
            tt_xxcstallhist.xxcstall_scurisswocyccnt      <> 0 or
			*/
			tt_xxcstallhist.xxcstall_scurisswosf          <> 0 or			
            tt_xxcstallhist.xxcstall_scurissworaw         <> 0 
 	    then do:   
	 
    for each tt_xxcstwoallhist where tt_xxcstwoallhist.xxcstwoallhist_comp   =  tt_xxcstallhist.xxcstall_part
 	                             and tt_xxcstwoallhist.xxcstwoallhist_site   =  tt_xxcstallhist.xxcstall_site 
 					             and tt_xxcstwoallhist.xxcstwoallhist_domain =  global_domain         
 					             and tt_xxcstwoallhist.xxcstwoallhist_entity =  tt_xxcstallhist.xxcstall_entity
 					             and tt_xxcstwoallhist.xxcstwoallhist_year   =  tt_xxcstallhist.xxcstall_year
 					             and tt_xxcstwoallhist.xxcstwoallhist_period =  tt_xxcstallhist.xxcstall_period
                                 and tt_xxcstwoallhist.xxcstwoallhist_complevel = cur_level
                                  .
			if debug-prog then 					  
 		  status default tt_xxcstwoallhist.xxcstwoallhist_par		  + " SF Allocation Rate". 
    /** allocation rate = component Manufacture variance amount / total iss-wo quantity tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty **/ 
    assign 
            tt_xxcstwoallhist.xxcstwoallhist_sratemusage       = tt_xxcstallhist.xxcstall_scurisswomusage       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratemrate        = tt_xxcstallhist.xxcstall_scurisswomrate        / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratemmthd        = tt_xxcstallhist.xxcstall_scurisswommthd        / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_mtl    = tt_xxcstallhist.xxcstall_scurisswommthd_mtl    / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_lbr    = tt_xxcstallhist.xxcstall_scurisswommthd_lbr    / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_bdn    = tt_xxcstallhist.xxcstall_scurisswommthd_bdn    / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
			tt_xxcstwoallhist.xxcstwoallhist_sratesubusage     = tt_xxcstallhist.xxcstall_scurisswosubusage     / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratesubrate      = tt_xxcstallhist.xxcstall_scurisswosubrate      / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratelbrusage     = tt_xxcstallhist.xxcstall_scurisswolbrusage     / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratelbrrate      = tt_xxcstallhist.xxcstall_scurisswolbrrate      / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratebdnusage     = tt_xxcstallhist.xxcstall_scurisswobdnusage     / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratebdnrate      = tt_xxcstallhist.xxcstall_scurisswobdnrate      / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj       = tt_xxcstallhist.xxcstall_scurisswocstadj       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_mtl   = tt_xxcstallhist.xxcstall_scurisswocstadj_mtl   / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_lbr   = tt_xxcstallhist.xxcstall_scurisswocstadj_lbr   / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_bdn   = tt_xxcstallhist.xxcstall_scurisswocstadj_bdn   / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_ovh   = tt_xxcstallhist.xxcstall_scurisswocstadj_ovh   / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_sub   = tt_xxcstallhist.xxcstall_scurisswocstadj_sub   / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            /*
			tt_xxcstwoallhist.xxcstwoallhist_sratecyccnt       = tt_xxcstallhist.xxcstall_scurisswocyccnt       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            */
			tt_xxcstwoallhist.xxcstwoallhist_sratecommon       = tt_xxcstallhist.xxcstall_scurisswocommon       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            
			tt_xxcstwoallhist.xxcstwoallhist_srateothers       = tt_xxcstallhist.xxcstall_scurisswoothers       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_srateraw          = tt_xxcstallhist.xxcstall_scurissworaw          / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
			tt_xxcstwoallhist.xxcstwoallhist_sratesf           = tt_xxcstallhist.xxcstall_scurisswosf           / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_mtl       = tt_xxcstallhist.xxcstall_scurisswosf_mtl       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_lbr       = tt_xxcstallhist.xxcstall_scurisswosf_lbr       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_bdn       = tt_xxcstallhist.xxcstall_scurisswosf_bdn       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_ovh       = tt_xxcstallhist.xxcstall_scurisswosf_ovh       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_sub       = tt_xxcstallhist.xxcstall_scurisswosf_sub       / tt_xxcstwoallhist.xxcstwoallhist_iss_pertotqty 
            
            tt_xxcstwoallhist.xxcstwoallhist_sratemusage       = if tt_xxcstwoallhist.xxcstwoallhist_sratemusage     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratemusage    
            tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_mtl    = if tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_mtl  = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_mtl     
            tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_lbr    = if tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_lbr  = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_lbr     
            tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_bdn    = if tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_bdn  = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_bdn     
            tt_xxcstwoallhist.xxcstwoallhist_sratemrate        = if tt_xxcstwoallhist.xxcstwoallhist_sratemrate      = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratemrate     
            tt_xxcstwoallhist.xxcstwoallhist_sratemmthd        = if tt_xxcstwoallhist.xxcstwoallhist_sratemmthd      = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratemmthd     
            tt_xxcstwoallhist.xxcstwoallhist_sratesubusage     = if tt_xxcstwoallhist.xxcstwoallhist_sratesubusage   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratesubusage  
            tt_xxcstwoallhist.xxcstwoallhist_sratesubrate      = if tt_xxcstwoallhist.xxcstwoallhist_sratesubrate    = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratesubrate   
            tt_xxcstwoallhist.xxcstwoallhist_sratelbrusage     = if tt_xxcstwoallhist.xxcstwoallhist_sratelbrusage   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratelbrusage  
            tt_xxcstwoallhist.xxcstwoallhist_sratelbrrate      = if tt_xxcstwoallhist.xxcstwoallhist_sratelbrrate    = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratelbrrate   
            tt_xxcstwoallhist.xxcstwoallhist_sratebdnusage     = if tt_xxcstwoallhist.xxcstwoallhist_sratebdnusage   = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratebdnusage  
            tt_xxcstwoallhist.xxcstwoallhist_sratebdnrate      = if tt_xxcstwoallhist.xxcstwoallhist_sratebdnrate    = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratebdnrate   
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj       = if tt_xxcstwoallhist.xxcstwoallhist_sratecstadj     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratecstadj    
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_mtl   = if tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_mtl = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_mtl
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_lbr   = if tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_lbr = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_lbr
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_bdn   = if tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_bdn = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_bdn
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_ovh   = if tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_ovh = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_ovh
            tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_sub   = if tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_sub = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_sub
            /*
			tt_xxcstwoallhist.xxcstwoallhist_sratecyccnt       = if tt_xxcstwoallhist.xxcstwoallhist_sratecyccnt     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratecyccnt     
			*/
			tt_xxcstwoallhist.xxcstwoallhist_sratecommon       = if tt_xxcstwoallhist.xxcstwoallhist_sratecommon     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratecommon     
            
            tt_xxcstwoallhist.xxcstwoallhist_srateothers       = if tt_xxcstwoallhist.xxcstwoallhist_srateothers     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_srateothers     
            tt_xxcstwoallhist.xxcstwoallhist_srateraw          = if tt_xxcstwoallhist.xxcstwoallhist_srateraw        = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_srateraw        
            
		    tt_xxcstwoallhist.xxcstwoallhist_sratesf           = if tt_xxcstwoallhist.xxcstwoallhist_sratesf         = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratesf         
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_mtl       = if tt_xxcstwoallhist.xxcstwoallhist_sratesf_mtl     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratesf_mtl     
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_lbr       = if tt_xxcstwoallhist.xxcstwoallhist_sratesf_lbr     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratesf_lbr     
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_bdn       = if tt_xxcstwoallhist.xxcstwoallhist_sratesf_bdn     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratesf_bdn     
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_ovh       = if tt_xxcstwoallhist.xxcstwoallhist_sratesf_ovh     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratesf_ovh     
            tt_xxcstwoallhist.xxcstwoallhist_sratesf_sub       = if tt_xxcstwoallhist.xxcstwoallhist_sratesf_sub     = ? then 0 else tt_xxcstwoallhist.xxcstwoallhist_sratesf_sub     
			
			.
 
         /** calculate current iss-wo alllocate amount for parent part */
        
        tt_xxcstwoallhist.xxcstwoallhist_scurmusage     =  tt_xxcstwoallhist.xxcstwoallhist_sratemusage     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurmrate      =  tt_xxcstwoallhist.xxcstwoallhist_sratemrate      * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurmmthd      =  tt_xxcstwoallhist.xxcstwoallhist_sratemmthd      * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurmmthd_mtl  =  tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_mtl  * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurmmthd_lbr  =  tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_lbr  * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurmmthd_bdn  =  tt_xxcstwoallhist.xxcstwoallhist_sratemmthd_bdn  * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scursubusage   =  tt_xxcstwoallhist.xxcstwoallhist_sratesubusage   * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scursubrate    =  tt_xxcstwoallhist.xxcstwoallhist_sratesubrate    * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurlbrusage   =  tt_xxcstwoallhist.xxcstwoallhist_sratelbrusage   * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurlbrrate    =  tt_xxcstwoallhist.xxcstwoallhist_sratelbrrate    * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurbdnusage   =  tt_xxcstwoallhist.xxcstwoallhist_sratebdnusage   * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurbdnrate    =  tt_xxcstwoallhist.xxcstwoallhist_sratebdnrate    * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurcstadj     =  tt_xxcstwoallhist.xxcstwoallhist_sratecstadj     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_mtl =  tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_mtl * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_lbr =  tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_lbr * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_bdn =  tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_bdn * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_ovh =  tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_ovh * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_sub =  tt_xxcstwoallhist.xxcstwoallhist_sratecstadj_sub * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        /*                                                                                                                                                
		tt_xxcstwoallhist.xxcstwoallhist_scurcyccnt     =  tt_xxcstwoallhist.xxcstwoallhist_sratecyccnt     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        */
		tt_xxcstwoallhist.xxcstwoallhist_scurcommon     =  tt_xxcstwoallhist.xxcstwoallhist_sratecommon     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
                                                                                                                           
		tt_xxcstwoallhist.xxcstwoallhist_scurothers     =  tt_xxcstwoallhist.xxcstwoallhist_srateothers     * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scurraw        =  tt_xxcstwoallhist.xxcstwoallhist_srateraw        * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
                                                                                                                                                         
		tt_xxcstwoallhist.xxcstwoallhist_scursf         = tt_xxcstwoallhist.xxcstwoallhist_sratesf          * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty). 
        tt_xxcstwoallhist.xxcstwoallhist_scursf_mtl     = tt_xxcstwoallhist.xxcstwoallhist_sratesf_mtl      * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scursf_lbr     = tt_xxcstwoallhist.xxcstwoallhist_sratesf_lbr      * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scursf_bdn     = tt_xxcstwoallhist.xxcstwoallhist_sratesf_bdn      * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scursf_ovh     = tt_xxcstwoallhist.xxcstwoallhist_sratesf_ovh      * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        tt_xxcstwoallhist.xxcstwoallhist_scursf_sub     = tt_xxcstwoallhist.xxcstwoallhist_sratesf_sub      * (- tt_xxcstwoallhist.xxcstwoallhist_iss_totqty).
        
                                                       
        tt_xxcstwoallhist.xxcstwoallhist_scurtot =   tt_xxcstwoallhist.xxcstwoallhist_scurmusage     +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurmrate      +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurmmthd      +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scursubusage   +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scursubrate    +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurlbrusage   +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurlbrrate    +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurbdnusage   +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurbdnrate    +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurcstadj     +
													 /*
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurcyccnt     +
                                                     */
													 tt_xxcstwoallhist.xxcstwoallhist_scurcommon     +
													 
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurothers     +
													 tt_xxcstwoallhist.xxcstwoallhist_scursf         +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurraw        
												     .
  
  tt_xxcstwoallhist.xxcstwoallhist_scur_mtl_tot =    tt_xxcstwoallhist.xxcstwoallhist_scurmusage     +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurmrate      +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurmmthd_mtl  +
												     tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_mtl + 
													 /*
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurcyccnt     +												 
                                                     */
													 tt_xxcstwoallhist.xxcstwoallhist_scurcommon     +
													 
                                                     tt_xxcstwoallhist.xxcstwoallhist_scursf_mtl     +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurraw        
												     .
												 
  tt_xxcstwoallhist.xxcstwoallhist_scur_lbr_tot =    tt_xxcstwoallhist.xxcstwoallhist_scurlbrusage   +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurlbrrate    +
													 tt_xxcstwoallhist.xxcstwoallhist_scurmmthd_lbr  +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_lbr +
													 tt_xxcstwoallhist.xxcstwoallhist_scursf_lbr     
                                                     .	
												 
  tt_xxcstwoallhist.xxcstwoallhist_scur_bdn_tot =    tt_xxcstwoallhist.xxcstwoallhist_scurbdnusage   +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurbdnrate    +
													 tt_xxcstwoallhist.xxcstwoallhist_scurmmthd_bdn  +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_bdn +
													 tt_xxcstwoallhist.xxcstwoallhist_scursf_bdn
												     .
												 
  tt_xxcstwoallhist.xxcstwoallhist_scur_ovh_tot =    tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_ovh +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurothers     +
													 tt_xxcstwoallhist.xxcstwoallhist_scursf_ovh
                                                     .
													 
  
  tt_xxcstwoallhist.xxcstwoallhist_scur_sub_tot =    tt_xxcstwoallhist.xxcstwoallhist_scursubusage   +
                                                     tt_xxcstwoallhist.xxcstwoallhist_scursubrate    +								
                                                     tt_xxcstwoallhist.xxcstwoallhist_scurcstadj_sub +
													 tt_xxcstwoallhist.xxcstwoallhist_scursf_sub
                                                     .
													 
 
  end. /** for each tt_xxcstwoallhist where tt_xxcstwoallhist.xxcstwoallhist_comp   =  tt_xxcstallhist.xxcstall_part
               calculate parent component current sf amount **/
 end. /** then do: **/
end. /** for each tt_xxcstallhist **/



/* Summary Current Manufacture Variance*/

/** Accumulate Parent Part Curren Manufacturing Variance to tt_xxcstallhist table **/
 			
 				
    for each tt_xxcstwoallhist where tt_xxcstwoallhist.xxcstwoallhist_site   =  i_site
 					             and tt_xxcstwoallhist.xxcstwoallhist_domain =  global_domain         
 					             and tt_xxcstwoallhist.xxcstwoallhist_entity =  i_entity
 					             and tt_xxcstwoallhist.xxcstwoallhist_year   =  i_year
 					             and tt_xxcstwoallhist.xxcstwoallhist_period =  i_period
								 and tt_xxcstwoallhist.xxcstwoallhist_complevel = cur_level
                                 .
		if debug-prog then 						 
	status default tt_xxcstwoallhist.xxcstwoallhist_par + " Accumulate Parent Part Curren Manufacturing Variance".							 
			find tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  = global_domain
 	                             and   tt_xxcstallhist.xxcstall_site    =  i_site
 				                 and   tt_xxcstallhist.xxcstall_year    =  i_year
 				                 and   tt_xxcstallhist.xxcstall_period  =  i_period
 				                 and   tt_xxcstallhist.xxcstall_entity  =  i_entity	
                                 and   tt_xxcstallhist.xxcstall_part = tt_xxcstwoallhist.xxcstwoallhist_par							 
                                 no-error.
            if available tt_xxcstallhist then 				   
            assign
			     tt_xxcstallhist.xxcstall_scursf      = tt_xxcstallhist.xxcstall_scursf + tt_xxcstwoallhist.xxcstwoallhist_scurtot		
                 tt_xxcstallhist.xxcstall_scursf_mtl  = tt_xxcstallhist.xxcstall_scursf_mtl + tt_xxcstwoallhist.xxcstwoallhist_scur_mtl_tot			
                 tt_xxcstallhist.xxcstall_scursf_lbr  = tt_xxcstallhist.xxcstall_scursf_lbr + tt_xxcstwoallhist.xxcstwoallhist_scur_lbr_tot
                 tt_xxcstallhist.xxcstall_scursf_bdn  = tt_xxcstallhist.xxcstall_scursf_bdn + tt_xxcstwoallhist.xxcstwoallhist_scur_bdn_tot
                 tt_xxcstallhist.xxcstall_scursf_ovh  = tt_xxcstallhist.xxcstall_scursf_ovh + tt_xxcstwoallhist.xxcstwoallhist_scur_ovh_tot
                 tt_xxcstallhist.xxcstall_scursf_sub  = tt_xxcstallhist.xxcstall_scursf_sub + tt_xxcstwoallhist.xxcstwoallhist_scur_sub_tot
			     .
   end. /* for each tt_xxcstwoallhist where tt_xxcstwoallhist.xxcstwoallhist_par   */

	   cur_level = cur_level + 1.
 /** Calculate Parent SF Variance allocation rate and amount **/
 /** when lowest level variance allocated to high level then the high level part is ready to allocate variance to next level */
 /**  Parent   component    level     **/
 /**  A         B            2        **/
 /**  A         C            0        **/
 /**  B         D            1        **/
 /**  B         K            1        **/
 /**  D         E            0        **/
 /**  H         F            0        **/

 /** but the last level's next level part have no  level number **/
 /** so when the cur_level > max level use parent parent to alllocate variance **/
 /** above comment is wrong change to below logic                */
 /** by gaoyan H is not the highest level and have no level mark */
 /**SF Variance Allocation Rate  = (begin SF variance + current SF variance) 
         / (begin inventory + current rct inventory) **/	 
  for each tt_xxcstwoallhist where tt_xxcstwoallhist.xxcstwoallhist_site   =  i_site
 					             and tt_xxcstwoallhist.xxcstwoallhist_domain =  global_domain         
 					             and tt_xxcstwoallhist.xxcstwoallhist_entity =  i_entity
 					             and tt_xxcstwoallhist.xxcstwoallhist_year   =  i_year
 					             and tt_xxcstwoallhist.xxcstwoallhist_period =  i_period
								 and tt_xxcstwoallhist.xxcstwoallhist_complevel = cur_level
								 break by tt_xxcstwoallhist.xxcstwoallhist_comp 
                                 .  
  if first-of(tt_xxcstwoallhist.xxcstwoallhist_comp) then do:							 
  for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  =  global_domain
 	                       and   tt_xxcstallhist.xxcstall_site    =  i_site
 				           and   tt_xxcstallhist.xxcstall_year    =  i_year
 				           and   tt_xxcstallhist.xxcstall_period  =  i_period
 				           and   tt_xxcstallhist.xxcstall_entity  =  i_entity	
						   and   tt_xxcstallhist.xxcstall_part    = tt_xxcstwoallhist.xxcstwoallhist_comp
						   . 
      totBeginvqty = 0.
	  totRctinvqty = 0.
	  if debug-prog then 
	  status default tt_xxcstallhist.xxcstall_part + " Calculate SFVAR Rate".
	  
	  Run GetBegInvQty(input tt_xxcstallhist.xxcstall_part,
	                   input tt_xxcstallhist.xxcstall_entity, 
					   input tt_xxcstallhist.xxcstall_site, 
					   input tt_xxcstallhist.xxcstall_year, 
					   input tt_xxcstallhist.xxcstall_period, 
					   output totBeginvqty).
	 
	  Run GetTotalRctQty(input tt_xxcstallhist.xxcstall_part,
	                     input tt_xxcstallhist.xxcstall_entity, 
					     input tt_xxcstallhist.xxcstall_site, 
					     input tt_xxcstallhist.xxcstall_year, 
					     input tt_xxcstallhist.xxcstall_period, 
						 output totRctinvqty). 

	 
		tt_xxcstallhist.xxcstall_sratesf     = if (tt_xxcstallhist.xxcstall_sbegsf + tt_xxcstallhist.xxcstall_scursf)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0 
											   else (tt_xxcstallhist.xxcstall_sbegsf + tt_xxcstallhist.xxcstall_scursf)
		                                        / (  totBeginvqty + totRctinvqty).
		tt_xxcstallhist.xxcstall_sratesf_mtl = if (tt_xxcstallhist.xxcstall_sbegsf_mtl + tt_xxcstallhist.xxcstall_scursf_mtl)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0
                                               else 
                                                  (tt_xxcstallhist.xxcstall_sbegsf_mtl + tt_xxcstallhist.xxcstall_scursf_mtl)
		                                        / (  totBeginvqty + totRctinvqty).											   
		tt_xxcstallhist.xxcstall_sratesf_bdn = if (tt_xxcstallhist.xxcstall_sbegsf_bdn + tt_xxcstallhist.xxcstall_scursf_bdn)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0
											   else 
											      (tt_xxcstallhist.xxcstall_sbegsf_bdn + tt_xxcstallhist.xxcstall_scursf_bdn)
		                                        / (  totBeginvqty + totRctinvqty).		
		tt_xxcstallhist.xxcstall_sratesf_lbr = if (tt_xxcstallhist.xxcstall_sbegsf_lbr + tt_xxcstallhist.xxcstall_scursf_lbr)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0
											   else
											      (tt_xxcstallhist.xxcstall_sbegsf_lbr + tt_xxcstallhist.xxcstall_scursf_lbr)
		                                        / (  totBeginvqty + totRctinvqty).											   	       
		tt_xxcstallhist.xxcstall_sratesf_ovh = if (tt_xxcstallhist.xxcstall_sbegsf_ovh + tt_xxcstallhist.xxcstall_scursf_ovh)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0
												else
												  (tt_xxcstallhist.xxcstall_sbegsf_ovh + tt_xxcstallhist.xxcstall_scursf_ovh)
		                                        / (  totBeginvqty + totRctinvqty).		
		tt_xxcstallhist.xxcstall_sratesf_sub = if (tt_xxcstallhist.xxcstall_sbegsf_sub + tt_xxcstallhist.xxcstall_scursf_sub)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0 
												else 
												  (tt_xxcstallhist.xxcstall_sbegsf_sub + tt_xxcstallhist.xxcstall_scursf_sub)
		                                        / (  totBeginvqty + totRctinvqty)
												.								
		/* need to validate 
		tt_xxcstallhist.xxcstall_srateraw = (tt_xxcstallhist.xxcstall_sbegraw + tt_xxcstallhist.xxcstall_scurraw)
		                                     / (  totBeginvqty + totRctinvqty).
		*/
		
       /** SF Variance Allocation amount  = SF Variance amount * ISSUE quantity **/
	   
	  totrqty = 0.
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-UNP",
	               output totrqty).
	   assign
         tt_xxcstallhist.xxcstall_scurissunpsf     = tt_xxcstallhist.xxcstall_sratesf     * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_mtl = tt_xxcstallhist.xxcstall_sratesf_mtl * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_lbr = tt_xxcstallhist.xxcstall_sratesf_lbr * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_bdn = tt_xxcstallhist.xxcstall_sratesf_bdn * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_ovh = tt_xxcstallhist.xxcstall_sratesf_ovh * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_sub = tt_xxcstallhist.xxcstall_sratesf_sub * (- totrqty).
      totrqty = 0.
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-WO",
	               output totrqty).
	           Run GetSameISSRCTWO(input tt_xxcstallhist.xxcstall_part,
                             input entity,
                             input site,
                             input li_year,
                             input li_period,
                             output totissqty).
				totrqty = totrqty - totissqty.				   
	  assign
         tt_xxcstallhist.xxcstall_scurisswosf     = tt_xxcstallhist.xxcstall_sratesf     * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_mtl = tt_xxcstallhist.xxcstall_sratesf_mtl * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_lbr = tt_xxcstallhist.xxcstall_sratesf_lbr * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_bdn = tt_xxcstallhist.xxcstall_sratesf_bdn * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_ovh = tt_xxcstallhist.xxcstall_sratesf_ovh * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_sub = tt_xxcstallhist.xxcstall_sratesf_sub * (- totrqty).
      totrqty = 0.  
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "INVLOSS",
	               output totrqty).
	  assign
         tt_xxcstallhist.xxcstall_scurcyccntsf     = tt_xxcstallhist.xxcstall_sratesf     * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_mtl = tt_xxcstallhist.xxcstall_sratesf_mtl * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_lbr = tt_xxcstallhist.xxcstall_sratesf_lbr * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_bdn = tt_xxcstallhist.xxcstall_sratesf_bdn * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_ovh = tt_xxcstallhist.xxcstall_sratesf_ovh * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_sub = tt_xxcstallhist.xxcstall_sratesf_sub * (- totrqty).
      totrqty = 0.  
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-SO",
	               output totrqty).
	  assign
         tt_xxcstallhist.xxcstall_scurisssosf     = tt_xxcstallhist.xxcstall_sratesf     * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_mtl = tt_xxcstallhist.xxcstall_sratesf_mtl * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_lbr = tt_xxcstallhist.xxcstall_sratesf_lbr * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_bdn = tt_xxcstallhist.xxcstall_sratesf_bdn * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_ovh = tt_xxcstallhist.xxcstall_sratesf_ovh * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_sub = tt_xxcstallhist.xxcstall_sratesf_sub * (- totrqty).

    end. /* for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain */
  end.  /* if first-of() */
end. 	/* for each tt_xxcstwoallhist */

 /** but the last level's next level have no  level number **/
 /** so when the cur_level > max level use parent parent to alllocate variance **/
 /** find the parent part have not in the component column  and allocate variance **/
  if cur_level > v_max then do:
/*** change logic to cover all part that need to allocate variance 
  for each tt_xxcstwoallhist where tt_xxcstwoallhist.xxcstwoallhist_site   =  i_site
 					             and tt_xxcstwoallhist.xxcstwoallhist_domain =  global_domain         
 					             and tt_xxcstwoallhist.xxcstwoallhist_entity =  i_entity
 					             and tt_xxcstwoallhist.xxcstwoallhist_year   =  i_year
 					             and tt_xxcstwoallhist.xxcstwoallhist_period =  i_period
								 break by tt_xxcstwoallhist.xxcstwoallhist_par
                                 .  
  if first-of(tt_xxcstwoallhist.xxcstwoallhist_par) 	
   and not can-find(first b_xxcstwoallhist  where b_xxcstwoallhist.xxcstwoallhist_site   =  i_site
 					                    and b_xxcstwoallhist.xxcstwoallhist_domain =  global_domain         
 					                    and b_xxcstwoallhist.xxcstwoallhist_entity =  i_entity
 					                    and b_xxcstwoallhist.xxcstwoallhist_year   =  i_year
 					                    and b_xxcstwoallhist.xxcstwoallhist_period =  i_period
								        and b_xxcstwoallhist.xxcstwoallhist_comp   =  tt_xxcstwoallhist.xxcstwoallhist_par)
								        then do:
  for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  =  global_domain
 	                       and   tt_xxcstallhist.xxcstall_site    =  i_site
 				           and   tt_xxcstallhist.xxcstall_year    =  i_year
 				           and   tt_xxcstallhist.xxcstall_period  =  i_period
 				           and   tt_xxcstallhist.xxcstall_entity  =  i_entity	
						   and   tt_xxcstallhist.xxcstall_part    = tt_xxcstwoallhist.xxcstwoallhist_par
						   . 

**/
  for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain  =  global_domain
 	                       and   tt_xxcstallhist.xxcstall_site    =  i_site
 				           and   tt_xxcstallhist.xxcstall_year    =  i_year
 				           and   tt_xxcstallhist.xxcstall_period  =  i_period
 				           and   tt_xxcstallhist.xxcstall_entity  =  i_entity	
						   and  
	  not can-find (first tt_xxcstwoallhist where tt_xxcstwoallhist.xxcstwoallhist_site   =  i_site
 					             and tt_xxcstwoallhist.xxcstwoallhist_domain =  global_domain         
 					             and tt_xxcstwoallhist.xxcstwoallhist_entity =  i_entity
 					             and tt_xxcstwoallhist.xxcstwoallhist_year   =  i_year
 					             and tt_xxcstwoallhist.xxcstwoallhist_period =  i_period
								 and tt_xxcstwoallhist.xxcstwoallhist_comp   =  tt_xxcstallhist.xxcstall_part)
				
                                 .  
      totBeginvqty = 0.
	  totRctinvqty = 0.
	  if debug-prog then 
	  status default tt_xxcstallhist.xxcstall_part + " Calculate last level SF Rate amount".
	  
	  Run GetBegInvQty(input tt_xxcstallhist.xxcstall_part,
	                   input tt_xxcstallhist.xxcstall_entity, 
					   input tt_xxcstallhist.xxcstall_site, 
					   input tt_xxcstallhist.xxcstall_year, 
					   input tt_xxcstallhist.xxcstall_period, 
					   output totBeginvqty).
	 
	  Run GetTotalRctQty(input tt_xxcstallhist.xxcstall_part,
	                     input tt_xxcstallhist.xxcstall_entity, 
					     input tt_xxcstallhist.xxcstall_site, 
					     input tt_xxcstallhist.xxcstall_year, 
					     input tt_xxcstallhist.xxcstall_period, 
						 output totRctinvqty). 

	 
		tt_xxcstallhist.xxcstall_sratesf     = if (tt_xxcstallhist.xxcstall_sbegsf + tt_xxcstallhist.xxcstall_scursf)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0 
											   else (tt_xxcstallhist.xxcstall_sbegsf + tt_xxcstallhist.xxcstall_scursf)
		                                        / (  totBeginvqty + totRctinvqty).
		tt_xxcstallhist.xxcstall_sratesf_mtl = if (tt_xxcstallhist.xxcstall_sbegsf_mtl + tt_xxcstallhist.xxcstall_scursf_mtl)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0
                                               else 
                                                  (tt_xxcstallhist.xxcstall_sbegsf_mtl + tt_xxcstallhist.xxcstall_scursf_mtl)
		                                        / (  totBeginvqty + totRctinvqty).											   
		tt_xxcstallhist.xxcstall_sratesf_bdn = if (tt_xxcstallhist.xxcstall_sbegsf_bdn + tt_xxcstallhist.xxcstall_scursf_bdn)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0
											   else 
											      (tt_xxcstallhist.xxcstall_sbegsf_bdn + tt_xxcstallhist.xxcstall_scursf_bdn)
		                                        / (  totBeginvqty + totRctinvqty).		
		tt_xxcstallhist.xxcstall_sratesf_lbr = if (tt_xxcstallhist.xxcstall_sbegsf_lbr + tt_xxcstallhist.xxcstall_scursf_lbr)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0
											   else
											      (tt_xxcstallhist.xxcstall_sbegsf_lbr + tt_xxcstallhist.xxcstall_scursf_lbr)
		                                        / (  totBeginvqty + totRctinvqty).											   	       
		tt_xxcstallhist.xxcstall_sratesf_ovh = if (tt_xxcstallhist.xxcstall_sbegsf_ovh + tt_xxcstallhist.xxcstall_scursf_ovh)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0
												else
												  (tt_xxcstallhist.xxcstall_sbegsf_ovh + tt_xxcstallhist.xxcstall_scursf_ovh)
		                                        / (  totBeginvqty + totRctinvqty).		
		tt_xxcstallhist.xxcstall_sratesf_sub = if (tt_xxcstallhist.xxcstall_sbegsf_sub + tt_xxcstallhist.xxcstall_scursf_sub)
		                                        / (  totBeginvqty + totRctinvqty) = ? then 0 
												else 
												  (tt_xxcstallhist.xxcstall_sbegsf_sub + tt_xxcstallhist.xxcstall_scursf_sub)
		                                        / (  totBeginvqty + totRctinvqty)
												.								
		/* need to validate 
		tt_xxcstallhist.xxcstall_srateraw = (tt_xxcstallhist.xxcstall_sbegraw + tt_xxcstallhist.xxcstall_scurraw)
		                                     / (  totBeginvqty + totRctinvqty).
		*/
		
       /** SF Variance Allocation amount  = SF Variance amount * ISSUE quantity **/
	   
	  totrqty = 0.
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-UNP",
	               output totrqty).
	   assign
         tt_xxcstallhist.xxcstall_scurissunpsf     = tt_xxcstallhist.xxcstall_sratesf     * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_mtl = tt_xxcstallhist.xxcstall_sratesf_mtl * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_lbr = tt_xxcstallhist.xxcstall_sratesf_lbr * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_bdn = tt_xxcstallhist.xxcstall_sratesf_bdn * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_ovh = tt_xxcstallhist.xxcstall_sratesf_ovh * (- totrqty)
         tt_xxcstallhist.xxcstall_scurissunpsf_sub = tt_xxcstallhist.xxcstall_sratesf_sub * (- totrqty).
      totrqty = 0.
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-WO",
	               output totrqty).
      Run GetSameISSRCTWO(input tt_xxcstallhist.xxcstall_part,
                             input entity,
                             input site,
                             input li_year,
                             input li_period,
                             output totissqty).
				totrqty = totrqty - totissqty.
	  assign
         tt_xxcstallhist.xxcstall_scurisswosf     = tt_xxcstallhist.xxcstall_sratesf     * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_mtl = tt_xxcstallhist.xxcstall_sratesf_mtl * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_lbr = tt_xxcstallhist.xxcstall_sratesf_lbr * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_bdn = tt_xxcstallhist.xxcstall_sratesf_bdn * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_ovh = tt_xxcstallhist.xxcstall_sratesf_ovh * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisswosf_sub = tt_xxcstallhist.xxcstall_sratesf_sub * (- totrqty).
      totrqty = 0.  
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "INVLOSS",
	               output totrqty).
	  assign
         tt_xxcstallhist.xxcstall_scurcyccntsf     = tt_xxcstallhist.xxcstall_sratesf     * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_mtl = tt_xxcstallhist.xxcstall_sratesf_mtl * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_lbr = tt_xxcstallhist.xxcstall_sratesf_lbr * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_bdn = tt_xxcstallhist.xxcstall_sratesf_bdn * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_ovh = tt_xxcstallhist.xxcstall_sratesf_ovh * (- totrqty)
         tt_xxcstallhist.xxcstall_scurcyccntsf_sub = tt_xxcstallhist.xxcstall_sratesf_sub * (- totrqty).
      totrqty = 0.  
	  Run GetTrQtySum(input tt_xxcstallhist.xxcstall_part,
	               input i_entity,
				   input i_site,
				   input i_year,
				   input i_period,
	               input "ISS-SO",
	               output totrqty).
	  assign
         tt_xxcstallhist.xxcstall_scurisssosf     = tt_xxcstallhist.xxcstall_sratesf     * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_mtl = tt_xxcstallhist.xxcstall_sratesf_mtl * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_lbr = tt_xxcstallhist.xxcstall_sratesf_lbr * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_bdn = tt_xxcstallhist.xxcstall_sratesf_bdn * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_ovh = tt_xxcstallhist.xxcstall_sratesf_ovh * (- totrqty)
         tt_xxcstallhist.xxcstall_scurisssosf_sub = tt_xxcstallhist.xxcstall_sratesf_sub * (- totrqty).

    end. /* for each tt_xxcstallhist where tt_xxcstallhist.xxcstall_domain */
	/*
  end.  /* if first-of() */
end. 	/* for each tt_xxcstwoallhist */
   */
end.  /** if cur_level = v_max then **/

	   if cur_level > v_max then leave.

	   
end. /**Repeat to v_max times **/   


/* BOM Level calculate */
PROCEDURE BOM_LEVEL.
DEFINE INPUT PARAMETER I_Par   LIKE ps_mstr.ps_par  NO-UNDO.
DEFINE INPUT PARAMETER level as integer no-undo.
    
    for each b_xxcstwoallhist where b_xxcstwoallhist.xxcstwoallhist_domain = global_domain 
	                            and b_xxcstwoallhist.xxcstwoallhist_site   = i_site
								and b_xxcstwoallhist.xxcstwoallhist_year   = i_year
	                            and b_xxcstwoallhist.xxcstwoallhist_period = i_period
								and b_xxcstwoallhist.xxcstwoallhist_entity = i_entity						 
                                and b_xxcstwoallhist.xxcstwoallhist_par    = i_par 
								.
    
    l_max = level.        
    if l_max > l_par_level then
    l_par_level = l_max.
                           
    run BOM_LEVEL(input b_xxcstwoallhist.xxcstwoallhist_comp, input level + 1).
          
    end.

END PROCEDURE.

PROCEDURE GetTrQtySum.
DEFINE INPUT PARAMETER I_Part  like pt_part.
DEFINE INPUT PARAMETER I_Entity  like en_entity.
DEFINE INPUT PARAMETER I_Site  like in_site.
DEFINE INPUT PARAMETER I_year  like glc_year.
DEFINE INPUT PARAMETER I_period  like glc_per.
DEFINE INPUT PARAMETER I_vartype as character.
DEFINE OUTPUT PARAMETER O_TOTQTY like ld_qty_oh.

  O_TOTQTY = 0.
  for each xxcsttrvar_mstr no-lock where xxcsttrvar_mstr.xxcsttrvar_domain = global_domain
                                     and xxcsttrvar_mstr.xxcsttrvar_site   = I_Site
 								     and xxcsttrvar_mstr.xxcsttrvar_part   =  I_part
 								     and xxcsttrvar_mstr.xxcsttrvar_year   =  I_year
 								     and xxcsttrvar_mstr.xxcsttrvar_period =  I_period
 								     and xxcsttrvar_mstr.xxcsttrvar_entity =   I_Entity
								     and xxcsttrvar_mstr.xxcsttrvar_var_type = I_vartype.
		if xxcsttrvar_mstr.xxcsttrvar_var_type = i_vartype then 
		 assign O_TOTQTY = xxcsttrvar_mstr.xxcsttrvar_qty.
   end.
   
END.


